package datatelpack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Main2DataTel {
	
public static void main (String [] args) throws Exception{
	
	String username ;
	int id = 0;
	int contatore = 1;
    String inflow = "telecomdata";
	  
	 try
	    {
	      // create a mysql database connection
	      String myDriver = "org.gjt.mm.mysql.Driver";
	      String myUrl = "jdbc:mysql://localhost/telecom";
	      Class.forName(myDriver);
	      Connection conn = DriverManager.getConnection(myUrl, "root", "SS7IAZ_*");
	      
	      Statement st = conn.createStatement();
	    
      File Folder = new File(inflow);
      File files[];
      files = Folder.listFiles();
     //br = null;

      if(files.length>1)
      {
          for(int i = 0;i<files.length; i++){
              System.out.println("reading...");
              System.out.println(files[i]);
              
              StringBuilder text = new StringBuilder();
              System.out.println(" indice i "+i);
              BufferedReader  br = new BufferedReader(new FileReader(files[i]));
    
              String line = "";	
              while((line = br.readLine())!=null){
              String [] s = line.split("\t");
              username = s[0];
              id++;
              
              try{
              
              st.executeUpdate("INSERT INTO user (username, id, contatore) "
        	          +"VALUES ('"+username+"', '"+id+"',  '"+contatore+"')");
              }catch (SQLException e){
            	  e.getCause();
            	  contatore++;
            	  st.executeUpdate("UPDATE user SET contatore ='"+contatore+"' " +
            	  		", WHERE username = '"+username+"'");
              }
              }
              br.close();
          }
      }
      
     	      System.out.println("record inserito");
	      conn.close();
	    }
		catch (Exception e){
			e.printStackTrace();
		}		
		
	}
		
}
